package com.cbl.v.ssczs.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.Toast;

import com.cbl.v.ssczs.R;
import com.cbl.v.ssczs.mode.net.HttpRequestForResponse;
import com.cbl.v.ssczs.mode.net.MyState;
import com.cbl.v.ssczs.mode.util.DialogUtils;
import com.cbl.v.ssczs.mode.util.Tools;
import com.cbl.v.ssczs.view.dialog.CommonDialog;

public class MainActivity extends BaseActivity implements CommonDialog.DialogClickListener {
    private WebView webView;
    private Handler handler;
    private CommonDialog dialog;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        hideTitleBar();
        handler = new Handler();
        webView = (WebView) findViewById(R.id.web);
        Tools.initWebView(webView);
    }

    @Override
    public void initData() {
        DialogUtils.showWithStatus(getContext());
        HttpRequestForResponse.getData(this, MyState.requestOne);
    }

    @Override
    public void initListener() {

    }


    @Override
    public void dialogSure(String data) {
        dialog.dismiss();
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(data);
        intent.setData(content_url);
        startActivity(intent);
    }

    @Override
    public void dialogCancel() {
        dialog.dismiss();
    }

    @Override
    public void dataBack(Object data, int order) {
        switch (order) {
            case MyState.requestOne:
                if (!TextUtils.isEmpty((String) data)) {
                    webView.loadUrl((String) data);
                }
                break;
        }
        super.dataBack(data, order);
    }

    @Override
    public void upDateApp(Object data) {
        if (!TextUtils.isEmpty((String) data)) {
            dialog = new CommonDialog(getContext());
            dialog.setAlert("有新版本是否更新?");
            dialog.setData((String) data);
            dialog.setDialogClickListener(this);
            dialog.show();
        }
        super.upDateApp(data);
    }

    @Override
    public void finishBack() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                DialogUtils.disMissDialog();
            }
        }, 1000);
        super.finishBack();
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            dialog.setAlert("是否退出该应用？");
//            dialog.setDialogClickListener(this);
//            dialog.show();
//            return true;
//        } else {// 如果不是back键正常响应
//            return super.onKeyDown(keyCode, event);
//        }
//    }

    private long lastBackPress;

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {

            if (System.currentTimeMillis() - lastBackPress < 1000) {
                super.onBackPressed();
            } else {
                lastBackPress = System.currentTimeMillis();
                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
